//    function fileSelect(){
//        var reader=new FileReader();
//        var file=document.querySelector("#files").files[0];
//        //var result=files.readAsText('movies.txt');
//        var arr;
//        reader.readAsText(file);
//        //reader.onload=function(){
//        var timer=setInterval(function(){
//            if(reader.result){
//                arr=reader.result.split("\n");
//                clearInterval(timer);
//            }
//        },20);
//    }
var read=document.querySelector("#read");
var movies;
var movieList=new List();
var customerList=new List();
var listOl=document.querySelector("#list");
read.onclick=function(){
    new Promise(function(resolve,reject){
        var xhr=new XMLHttpRequest();
        xhr.open('GET','movies.txt',true);
        xhr.send(null);
        xhr.onreadystatechange=function () {
            if (xhr.status == 200 && xhr.readyState == 4) {
                movies= xhr.responseText.split("\n");
                resolve();
            }
        }
    }).then(function(){
        for(var i=0;i<movies.length;i++){
            movieList.append(movies[i]);
        }
    }).then(function(){
        // console.log(movies);
        // console.log(movieList);
        displayList(movieList);
        checkOut('mengzhuo','《一天》',movieList,customerList);
        displayList(customerList);
        //console.log(customerList.getElement())
    });
};
//提供一个显示当前电影的函数
function displayList(list){
    var str='';
//        console.log(list.length());
//        console.log(list.listSize);
//        console.log(list.currPos())
    list.front();
    for(var i=0;i<list.length();i++){
        str+=list.getElement()+"\n";
        list.next();
    }
    listOl.innerHTML=str;
}
//创建消费者，名字和她所拥有的电影
function Customer(name,movie){
    this.name=name;
    this.movie=movie;
}
//创建一个允许用户检测当前有什么电影可以租赁
//思想：第一：先创建一个新的消费者对象，把这个对象放进对象列表中，然后在把他所要看的电影从电影列表中删除
function checkOut(name,movie,movieList,customerList){
    if(movieList.contains(movie)){
        var c=new Customer(name,movie);
        customerList.append(c);
        movieList.remove(movie);
    }else{
        console.log(movie +"is not available");
    }
}


